package com.ty.bigevent.controller;

import com.ty.bigevent.model.dto.CategoryUpdateRequest;
import com.ty.bigevent.model.entry.Category;
import com.ty.bigevent.model.vo.CategoryVO;
import com.ty.bigevent.service.CategoryService;
import com.ty.bigevent.utils.Result;
import com.ty.bigevent.utils.ThreadLocalUtil;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/category")
@Slf4j
@AllArgsConstructor
public class categoryController {
    private final CategoryService categoryService;
    @PostMapping("")
    public Result addCategory(@RequestBody @Validated Map<String,String>  params)
    {
        String categoryName = params.get("categoryName");
        String categoryAlias = params.get("categoryAlias");
        categoryService.addCategory(categoryName,categoryAlias);
        return Result.success();
    }

    @GetMapping("")
    public Result listCategory()
    {
        List<CategoryVO> categoryList = categoryService.listCategory();
        return Result.success(categoryList);
    }
    @GetMapping("/detail")
    public Result detailCategory(@RequestParam("id") Integer id)
    {
        // id不能为空且>0
        if (id == null || id <= 0)
        {
            return Result.error("参数错误");
        }
        CategoryVO category = categoryService.getCategoryById(id);
        return Result.success(category);
    }

    @PutMapping("")
    public Result updateCategory(@RequestBody @Validated CategoryUpdateRequest  params){
        categoryService.updateCategory(params);
        return Result.success();
    }
    @DeleteMapping()
    public Result deleteCategory(@RequestParam("id") Integer id)
        {
            if (id == null || id <= 0)
            {
                return Result.error("参数错误");
            }
            categoryService.deleteCategoryById(id);
            return Result.success();
        }
}
